package com.kye.express.business.deposit.presenter;

import com.kye.express.business.deposit.fragment.DepositConsumptionFragment;
import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.LifecycleOpenApiResultObserver;
import com.kye.express.contract.base.view.IView;
import com.kye.model.UserSession;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.deposit.RequestDepositDetailed;
import com.kye.model.bean.deposit.ResultDepositDetailedBean;
import com.kye.model.bean.user.User;
import com.kye.model.datasource.DepositDataSource;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 预存运费-消费记录 Presenter
 * <p>
 * Created by SkyYu on 2018/1/17
 */
public interface DepositConsumptionContract {

    interface DepositConsumptionView extends IView {
        void onUserLoad(User user);

        void onConsumptionLoading();

        void onConsumptionComplete(boolean isLoadMore);

        void onConsumptionSuccess(OpenApiResult<ResultDepositDetailedBean> bean, boolean isFirstIn, boolean isLoadMore);

        void onConsumptionApiError(OpenApiResult<ResultDepositDetailedBean> result, int code, String msg);

        void onConsumptionOtherError(Throwable e);
    }

    class DepositConsumptionPresenter extends BasePresenter<DepositConsumptionView> {

        DepositDataSource dataSource;
        DepositConsumptionFragment mCurrentFragment;

        public DepositConsumptionPresenter(DepositConsumptionFragment fragment) {
            dataSource = new DepositDataSource();
            mCurrentFragment = fragment;
        }

        @Override
        public void bind(DepositConsumptionView view) {
            super.bind(view);
            UserSession.getInstance()
                    .getUserForRx()
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((user) ->
                            getView().onUserLoad(user)
                    );
        }

        public void requestToDepositDetailed(boolean isAutoLoading, boolean isFirstIn, boolean isReFresh, boolean isLoadMore, String phone, String companyName, int pageIndex, int pageSize, String startTime, String endTime) {
            dataSource.depositDetailed(new RequestDepositDetailed(phone, companyName, pageIndex, pageSize, startTime, endTime))
                    .compose(getView().bindToLifecycleDestroy())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new LifecycleOpenApiResultObserver<ResultDepositDetailedBean>(this) {
                        @Override
                        public void onStart(Disposable d) {
                            if (isAutoLoading) {
                                getView().onConsumptionLoading();
                            }
                        }

                        @Override
                        public void onLoaded() {
                            getView().onConsumptionComplete(isLoadMore);
                        }

                        @Override
                        public void onSuccess(OpenApiResult<ResultDepositDetailedBean> result, ResultDepositDetailedBean record) {
                            if (isReFresh) {
                                resetPageIndex();
                                setCurrentPageIndex(mCurrentFragment.currentPageIndex + 1);
                            } else {
                                setCurrentPageIndex(mCurrentFragment.currentPageIndex + 1);
                            }
                            getView().onConsumptionSuccess(result, isFirstIn, isLoadMore);
                        }

                        @Override
                        public void onApiError(OpenApiResult<ResultDepositDetailedBean> result, int code, String msg) {
                            getView().onConsumptionApiError(result,code,msg);
                    }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onConsumptionOtherError(e);
                        }
                    });
        }

        public void refreshData(boolean isAutoLoading, boolean isFirstIn, boolean isReFresh, boolean isLoadMore, String phone, String companyName, int pageIndex, int pageSize, String startTime, String endTime) {
            requestToDepositDetailed(isAutoLoading, isFirstIn, isReFresh, isLoadMore, phone, companyName, pageIndex, pageSize, startTime, endTime);
        }

        public void loadMore(boolean isAutoLoading, boolean isFirstIn, boolean isReFresh, boolean isLoadMore, String phone, String companyName, int pageIndex, int pageSize, String startTime, String endTime) {
            requestToDepositDetailed(isAutoLoading, isFirstIn, isReFresh, isLoadMore, phone, companyName, pageIndex, pageSize, startTime, endTime);
        }

        void resetPageIndex() {
            setCurrentPageIndex(1);
        }

        void setCurrentPageIndex(int currentPage) {
            mCurrentFragment.currentPageIndex = currentPage;
        }
    }
}